﻿using System;
using System.Collections.Generic;
using System.Linq;
using CL_VisionCommu_Test.Helper;
using CLCommuConfig;
using CLVisionCommu;
using ImTools;

namespace CL_VisionCommu_Test.Models
{
    public class Slave_Communication : Abstract_Communication
    {
        public override string Init(string Path, string mark)
        {
            platForm = mark;
            CLVisionCommuCaller.NC_or_TS = mark; // 1: NC, 2: TS
            bool ret = CLVisionCommuCaller.Init(Path, out int code, out string msg, out bool occurred);
            return msg;
        }

        public override double ReadConstantValue(int Index, string ConstantName)
        {
            try
            {
                CLVisionCommuCaller.ReceiveConstantData(Index, ConstantName, out double value, out int Code, out string Msg, out bool Occurred);
                return value;
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override bool SendConstantValue(int Index, string ConstantName, double Value)
        {
            try
            {
                return CLVisionCommuCaller.SendConstantData(Index, ConstantName, Value, out int ErrorCode, out string ErrorText, out bool Occurred);
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override List<string> ReadObjectValue(int Index, ObjectItems objectItems)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < objectItems.ItemsCount; i++)
            {
                string buffer = "[";
                CLVisionCommuCaller.avcGetItemData(Index, objectItems.ObjectID, i, (int)((DataType)Enum.Parse(typeof(DataType), objectItems.Items[i].Type)), objectItems.Items[i].Array, out object Data, out int code, out string msg, out bool Occurred);
                if (objectItems.Items[i].Array > 1)
                {
                    List<string> value = new List<string>();
                    for (int index = 0; index < objectItems.Items[i].Array; index++)
                    {
                        switch (((DataType)Enum.Parse(typeof(DataType), objectItems.Items[i].Type)))
                        {
                            case DataType.BOOL:
                                if (Data is object[] bufferBoolValue)
                                {
                                    value.Add(bufferBoolValue[index].ToString());
                                }
                                break;
                            case DataType.DOUBLE:
                                if (Data is object[] bufferDoubleValue)
                                {
                                    value.Add(bufferDoubleValue[index].ToString());
                                }
                                break;
                            case DataType.FLOAT:
                                if (Data is object[] bufferFloatValue)
                                {
                                    value.Add(bufferFloatValue[index].ToString());
                                }
                                break;
                            case DataType.STRING:
                                value.Add(((string[])Data)[index].ToString());
                                break;
                            case DataType.INT:
                                if (Data is object[] bufferIntValue)
                                {
                                    value.Add(bufferIntValue[index].ToString());
                                }
                                break;
                            case DataType.USHORT:
                                if (Data is object[] bufferUshortValue)
                                {
                                    value.Add(bufferUshortValue[index].ToString());
                                }
                                break;
                        }

                    }
                    buffer = buffer + String.Join(",", value.ToArray()) + "]";
                }
                else
                {
                    buffer = Data.ToString();
                }
                list.Add(buffer);
            }
            return list;
        }

        public override bool SendObjectValue(int Index, ObjectItems objectItems)
        {
            try
            {
                bool ret = true;
                for (int i = 0; i < objectItems.ItemsCount; i++)
                {
                    DataType type = (DataType)Enum.Parse(typeof(DataType), objectItems.Items[i].Type);
                    object data = null;
                    string[] value = null;
                    bool array = false;
                    if (objectItems.Items[i].Default.StartsWith("["))
                    {
                        array = true;
                        value = objectItems.Items[i].Default.TrimEnd(']').TrimStart('[').Split(',');
                    }
                    else
                        value = new string[] { objectItems.Items[i].Default };
                    switch (type)
                    {
                        case DataType.USHORT:
                            if (array)
                            {
                                data = Array.ConvertAll(value, ushort.Parse);
                            }
                            else
                                data = Convert.ToUInt16(value[0]);
                            break;
                        case DataType.INT:
                            if (array)
                            {
                                data = Array.ConvertAll(value, int.Parse);
                            }
                            else
                                data = Convert.ToInt32(value[0]);
                            break;
                        case DataType.FLOAT:
                            if (array)
                            {
                                data = Array.ConvertAll(value, float.Parse);
                            }
                            else
                                data = Convert.ToSingle(value[0]);
                            break;
                        case DataType.DOUBLE:
                            if (array)
                            {
                                data = Array.ConvertAll(value, double.Parse);
                            }
                            else
                                data = Convert.ToDouble(value[0]);
                            break;
                        case DataType.STRING:
                            data = objectItems.Items[i].Default;
                            break;
                        case DataType.BOOL:
                            if (array)
                            {
                                data = Array.ConvertAll(value, bool.Parse);
                            }
                            else
                                data = Convert.ToBoolean(value[0]);
                            break;
                        default:
                            break;
                    }
                    ret &= CLVisionCommuCaller.avcSetItemData(Index, objectItems.ObjectID, i, (int)type, objectItems.Items[i].Array, data, out int code, out string msg, out bool Occurred);
                }
                return ret;
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override string GetStatus(int Index)
        {
            try
            {
                if (platForm == "TS")
                {
                    string Command = "Command: " + ReadConstantValue(Index, "Command").ToString() + "\r\n";
                    string ManFctNo = "ManFctNo: " + ReadConstantValue(Index, "ManFctNo").ToString() + "\r\n";
                    string ParaDinit1 = "ParaDinit1: " + ReadConstantValue(Index, "ParaDinit1").ToString() + "\r\n";
                    string ParaDinit2 = "ParaDinit2: " + ReadConstantValue(Index, "ParaDinit2").ToString() + "\r\n";
                    string ParaDinit3 = "ParaDinit3: " + ReadConstantValue(Index, "ParaDinit3").ToString() + "\r\n";
                    string ParaDinit4 = "ParaDinit4: " + ReadConstantValue(Index, "ParaDinit4").ToString() + "\r\n";
                    string ParaReal1 = "ParaReal1: " + ReadConstantValue(Index, "ParaReal1").ToString() + "\r\n";
                    string ParaReal2 = "ParaReal2: " + ReadConstantValue(Index, "ParaReal2").ToString() + "\r\n";
                    string ParaReal3 = "ParaReal3: " + ReadConstantValue(Index, "ParaReal3").ToString() + "\r\n";
                    string ParaReal4 = "ParaReal4: " + ReadConstantValue(Index, "ParaReal4").ToString() + "\r\n";
                    string Para1 = "Para1: " + ReadConstantValue(Index, "Para1").ToString() + "\r\n";
                    string Para2 = "Para2: " + ReadConstantValue(Index, "Para2").ToString() + "\r\n";
                    string IHandShake00 = "HandShake00: " + ReadConstantValue(Index, "HandShake00").ToString() + "\r\n";
                    string IHandShake01 = "HandShake01: " + ReadConstantValue(Index, "HandShake01").ToString() + "\r\n";
                    string IHandShake02 = "HandShake02: " + ReadConstantValue(Index, "HandShake02").ToString() + "\r\n";
                    string IHandShake03 = "HandShake03: " + ReadConstantValue(Index, "HandShake03").ToString() + "\r\n";
                    string IHandShake04 = "HandShake04: " + ReadConstantValue(Index, "HandShake04").ToString() + "\r\n";
                    string IHandShake05 = "HandShake05: " + ReadConstantValue(Index, "HandShake05").ToString() + "\r\n";
                    string IHandShake06 = "HandShake06: " + ReadConstantValue(Index, "HandShake06").ToString() + "\r\n";
                    string IHandShake07 = "HandShake07: " + ReadConstantValue(Index, "HandShake07").ToString() + "\r\n";
                    string StartSingle = "StartSingle: " + ReadConstantValue(Index, "StartSingle").ToString() + "\r\n";
                    string _Continuous = "Continuous: " + ReadConstantValue(Index, "Continuous").ToString() + "\r\n";
                    string CommandOnce = "CommandOnce: " + ReadConstantValue(Index, "CommandOnce").ToString() + "\r\n";
                    string BasSet = "BasSet: " + ReadConstantValue(Index, "BasSet").ToString() + "\r\n";
                    string WrkSet = "WrkSet: " + ReadConstantValue(Index, "WrkSet").ToString() + "\r\n";
                    return Command + ManFctNo + ParaDinit1 + ParaDinit2 + ParaDinit3 + ParaDinit4 + ParaReal1 + ParaReal2 + ParaReal3 + ParaReal4 + Para1 + Para2 + IHandShake00 + IHandShake01
                        + IHandShake02 + IHandShake03 + IHandShake04 + IHandShake05 + IHandShake06 + IHandShake07 + StartSingle + _Continuous + CommandOnce + BasSet + WrkSet;
                }
                else
                {
                    string RunStates = "Start check: " + ReadConstantValue(Index, "Start check").ToString() + "\r\n";
                    string TestplanStates = "Execute command: " + ReadConstantValue(Index, "Execute command").ToString() + "\r\n";
                    string ResultStates = "Command code: " + ReadConstantValue(Index, "Command code").ToString() + "\r\n";
                    string Mode = "Command parameter: " + ReadConstantValue(Index, "Command parameter").ToString() + "\r\n";
                    string HandShake00 = "IHandShake00: " + ReadConstantValue(Index, "IHandShake00").ToString() + "\r\n";
                    string HandShake01 = "IHandShake01: " + ReadConstantValue(Index, "IHandShake01").ToString() + "\r\n";
                    string HandShake02 = "IHandShake02: " + ReadConstantValue(Index, "IHandShake02").ToString() + "\r\n";
                    string HandShake03 = "IHandShake03: " + ReadConstantValue(Index, "IHandShake03").ToString() + "\r\n";
                    string HandShake04 = "IHandShake04: " + ReadConstantValue(Index, "IHandShake04").ToString() + "\r\n";
                    string HandShake05 = "IHandShake05: " + ReadConstantValue(Index, "IHandShake05").ToString() + "\r\n";
                    string HandShake06 = "IHandShake06: " + ReadConstantValue(Index, "IHandShake06").ToString() + "\r\n";
                    string HandShake07 = "IHandShake07: " + ReadConstantValue(Index, "IHandShake07").ToString() + "\r\n";
                    return RunStates + TestplanStates + ResultStates + Mode + HandShake00 + HandShake01
                        + HandShake02 + HandShake03 + HandShake04 + HandShake05 + HandShake06 + HandShake07;
                }

            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override bool SendConstantValue(int Index, string ConstantName, double Value, bool mark)
        {
            try
            {
                bool ret = CLVisionCommuCaller.SendConstantData(Index, ConstantName, Value, out int ErrorCode, out string ErrorText, out bool Occurred); ;
                return ret;
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override double ReadConstantValue(int Index, string ConstantName, bool mark)
        {
            throw new NotImplementedException();
        }
    }
}
